//
//  TableVC+Service.m
//  
//  jhf
//
//  Created by 计海峰 on 2020/8/19.
//

#import "TableVC+Service.h"
#import "ActionModel.h"
#import <CooperSDK/CooperSDK.h>

@implementation TableVC (Service)

// MARK:- service
- (void)hiddenKeyboard:(id)any{
    [self.view endEditing:true];
}

- (void)reloadData:(id)any {
    [self.vm refreshData:nil];
}

- (void)reForRows {
    [self.vm forRows];
}

- (void)reloadList:(id)any {
    [self.delayReloadSubject sendNext:@(++self.reloadCount)];
//    MAIN(^{
//        [self.tableview reloadData];
//    });
}

//
- (NSString *)anyForKey:(id)any{
    NSString *key;
    if (!any) {
        return key;
    }
    if (any && [any isKindOfClass:NSString.class]) {
        key = (NSString *)any;
    }
    if (any && [any isKindOfClass:ActionModel.class]) {
        ActionModel *action = (ActionModel *)any;
        key = action.key;
    }
    return key;
}

- (void)reloadSection:(id)any{
    if (self.isRefreshIng) { /// tableview 即将刷新或者正在刷新
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.throttleTime + 0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self reloadSection:any];
        });
        return;
    }
    NSString *key = [self anyForKey:any];
    NSIndexPath *indexPath = [self.vm searchRow:key];
    if (indexPath) {
        MAIN(^{
            [self.tableview beginUpdates];
            [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableview endUpdates];
        });
    }
}

- (void)reloadRow:(id)any{
    if (self.isRefreshIng) { /// tableview 即将刷新或者正在刷新
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.throttleTime + 0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self reloadRow:any];
        });
        return;
    }
    NSString *key = [self anyForKey:any];
    NSIndexPath *indexPath = [self.vm searchRow:key];
    if (indexPath) {
        MAIN(^{
            [self.tableview beginUpdates];
            [self.tableview reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableview endUpdates];
        });
    }
}

- (void)refreshRowHeight:(id)any{
    if (self.isRefreshIng) { /// tableview 即将刷新或者正在刷新
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.throttleTime + 0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self refreshRowHeight:any];
        });
        return;
    }
    NSString *key = [self anyForKey:any];
    NSIndexPath *indexPath = [self.vm searchRow:key];
    if (indexPath) {
        Section *section = [self.vm.sections safeObjectAtIndex:indexPath.section];
        if (!section) {
            return;
        }
        Row *row = [section.rows safeObjectAtIndex:indexPath.row];
        if (!row) {
            return;
        }
        row.height = row.model.cellHeight;
        MAIN(^{
            [UIView performWithoutAnimation:^{
                [self.tableview beginUpdates];
                [self.tableview reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                [self.tableview endUpdates];
            }];
        });
    }
}

- (void)refreshUpdates:(id)any{
    MAIN(^{
        [self.tableview beginUpdates];
        [self.tableview endUpdates];
    });
}

@end
